<!DOCTYPE html>
<html lang="zh-CN">
<head>
  <meta charset="UTF-8">
<meta name="viewport" content="width=device-width, initial-scale=1, maximum-scale=2">
<meta name="theme-color" content="#222">
<meta name="generator" content="Hexo 4.2.0">
  <link rel="apple-touch-icon" sizes="180x180" href="/images/logo.png">
  <link rel="icon" type="image/png" sizes="32x32" href="/images/logo.png">
  <link rel="icon" type="image/png" sizes="16x16" href="/images/logo.png">
  <link rel="mask-icon" href="/images/logo.png" color="#222">

<link rel="stylesheet" href="/css/main.css">


<link rel="stylesheet" href="/lib/font-awesome/css/font-awesome.min.css">

<script id="hexo-configurations">
    var NexT = window.NexT || {};
    var CONFIG = {"hostname":"gitee.com","root":"/","scheme":"Pisces","version":"7.7.2","exturl":false,"sidebar":{"position":"left","display":"post","padding":18,"offset":12,"onmobile":false},"copycode":{"enable":false,"show_result":false,"style":null},"back2top":{"enable":true,"sidebar":false,"scrollpercent":false},"bookmark":{"enable":false,"color":"#222","save":"auto"},"fancybox":false,"mediumzoom":false,"lazyload":false,"pangu":false,"comments":{"style":"tabs","active":null,"storage":true,"lazyload":false,"nav":null},"algolia":{"hits":{"per_page":10},"labels":{"input_placeholder":"Search for Posts","hits_empty":"We didn't find any results for the search: ${query}","hits_stats":"${hits} results found in ${time} ms"}},"localsearch":{"enable":false,"trigger":"auto","top_n_per_article":1,"unescape":false,"preload":false},"motion":{"enable":true,"async":false,"transition":{"post_block":"fadeIn","post_header":"slideDownIn","post_body":"slideDownIn","coll_header":"slideLeftIn","sidebar":"slideUpIn"}},"path":"./public/search.xml"};
  </script>

  <meta name="description" content="进阶对 JavaScript 函数的理解与运用：this指向、arguments、调用函数的不同方式、函数的上下文">
<meta property="og:type" content="article">
<meta property="og:title" content="函数进阶：理解函数的调用">
<meta property="og:url" content="https://gitee.com/mamba666/mamba666.git/2021/04/09/%E5%87%BD%E6%95%B0%E8%BF%9B%E9%98%B6%EF%BC%9A%E7%90%86%E8%A7%A3%E5%87%BD%E6%95%B0%E7%9A%84%E8%B0%83%E7%94%A8/index.html">
<meta property="og:site_name" content="共你云雨">
<meta property="og:description" content="进阶对 JavaScript 函数的理解与运用：this指向、arguments、调用函数的不同方式、函数的上下文">
<meta property="og:locale" content="zh_CN">
<meta property="article:published_time" content="2021-04-09T02:30:08.745Z">
<meta property="article:modified_time" content="2021-04-12T02:29:51.429Z">
<meta property="article:author" content="Edison">
<meta name="twitter:card" content="summary">

<link rel="canonical" href="https://gitee.com/mamba666/mamba666.git/2021/04/09/%E5%87%BD%E6%95%B0%E8%BF%9B%E9%98%B6%EF%BC%9A%E7%90%86%E8%A7%A3%E5%87%BD%E6%95%B0%E7%9A%84%E8%B0%83%E7%94%A8/">


<script id="page-configurations">
  // https://hexo.io/docs/variables.html
  CONFIG.page = {
    sidebar: "",
    isHome : false,
    isPost : true
  };
</script>

  <title>函数进阶：理解函数的调用 | 共你云雨</title>
  






  <noscript>
  <style>
  .use-motion .brand,
  .use-motion .menu-item,
  .sidebar-inner,
  .use-motion .post-block,
  .use-motion .pagination,
  .use-motion .comments,
  .use-motion .post-header,
  .use-motion .post-body,
  .use-motion .collection-header { opacity: initial; }

  .use-motion .site-title,
  .use-motion .site-subtitle {
    opacity: initial;
    top: initial;
  }

  .use-motion .logo-line-before i { left: initial; }
  .use-motion .logo-line-after i { right: initial; }
  </style>
</noscript>

</head>

<body itemscope itemtype="http://schema.org/WebPage">
  <div class="container use-motion">
    <div class="headband"></div>
	<a href="https://github.com/mamba666" target="_blank" rel="noopener" class="github-corner" aria-label="View source on GitHub"><svg width="80" height="80" viewBox="0 0 250 250" style="fill:#151513; color:#fff; position: absolute; top: 0; border: 0; right: 0;" aria-hidden="true"><path d="M0,0 L115,115 L130,115 L142,142 L250,250 L250,0 Z"></path><path d="M128.3,109.0 C113.8,99.7 119.0,89.6 119.0,89.6 C122.0,82.7 120.5,78.6 120.5,78.6 C119.2,72.0 123.4,76.3 123.4,76.3 C127.3,80.9 125.5,87.3 125.5,87.3 C122.9,97.6 130.6,101.9 134.4,103.2" fill="currentColor" style="transform-origin: 130px 106px;" class="octo-arm"></path><path d="M115.0,115.0 C114.9,115.1 118.7,116.5 119.8,115.4 L133.7,101.6 C136.9,99.2 139.9,98.4 142.2,98.6 C133.8,88.0 127.5,74.4 143.8,58.0 C148.5,53.4 154.0,51.2 159.7,51.0 C160.3,49.4 163.2,43.6 171.4,40.1 C171.4,40.1 176.1,42.5 178.8,56.2 C183.1,58.6 187.2,61.8 190.9,65.4 C194.5,69.0 197.7,73.2 200.1,77.6 C213.8,80.2 216.3,84.9 216.3,84.9 C212.7,93.1 206.9,96.0 205.4,96.6 C205.1,102.4 203.0,107.8 198.3,112.5 C181.9,128.9 168.3,122.5 157.7,114.1 C157.9,116.9 156.7,120.9 152.7,124.9 L141.0,136.5 C139.8,137.7 141.6,141.9 141.8,141.8 Z" fill="currentColor" class="octo-body"></path></svg></a><style>.github-corner:hover .octo-arm{animation:octocat-wave 560ms ease-in-out}@keyframes octocat-wave{0%,100%{transform:rotate(0)}20%,60%{transform:rotate(-25deg)}40%,80%{transform:rotate(10deg)}}@media (max-width:500px){.github-corner:hover .octo-arm{animation:none}.github-corner .octo-arm{animation:octocat-wave 560ms ease-in-out}}</style>

    <header class="header" itemscope itemtype="http://schema.org/WPHeader">
      <div class="header-inner"><div class="site-brand-container">
  <div class="site-nav-toggle">
    <div class="toggle" aria-label="切换导航栏">
      <span class="toggle-line toggle-line-first"></span>
      <span class="toggle-line toggle-line-middle"></span>
      <span class="toggle-line toggle-line-last"></span>
    </div>
  </div>

  <div class="site-meta">

    <div>
      <a href="/" class="brand" rel="start">
        <span class="logo-line-before"><i></i></span>
        <span class="site-title">共你云雨</span>
        <span class="logo-line-after"><i></i></span>
      </a>
    </div>
  </div>

  <div class="site-nav-right">
    <div class="toggle popup-trigger">
    </div>
  </div>
</div>


<nav class="site-nav">
  
  <ul id="menu" class="menu">
        <li class="menu-item menu-item-home">

    <a href="/" rel="section"><i class="fa fa-fw fa-home"></i>首页</a>

  </li>
        <li class="menu-item menu-item-tags">

    <a href="/tags/" rel="section"><i class="fa fa-fw fa-tags"></i>标签</a>

  </li>
        <li class="menu-item menu-item-categories">

    <a href="/categories/" rel="section"><i class="fa fa-fw fa-th"></i>分类</a>

  </li>
        <li class="menu-item menu-item-archives">

    <a href="/archives/" rel="section"><i class="fa fa-fw fa-archive"></i>归档</a>

  </li>
  </ul>

</nav>
</div>
    </header>

    
  <div class="back-to-top">
    <i class="fa fa-arrow-up"></i>
    <span>0%</span>
  </div>


    <main class="main">
      <div class="main-inner">
        <div class="content-wrap">
          

          <div class="content">
            

  <div class="posts-expand">
      
  
  
  <article itemscope itemtype="http://schema.org/Article" class="post-block " lang="zh-CN">
    <link itemprop="mainEntityOfPage" href="https://gitee.com/mamba666/mamba666.git/2021/04/09/%E5%87%BD%E6%95%B0%E8%BF%9B%E9%98%B6%EF%BC%9A%E7%90%86%E8%A7%A3%E5%87%BD%E6%95%B0%E7%9A%84%E8%B0%83%E7%94%A8/">

    <span hidden itemprop="author" itemscope itemtype="http://schema.org/Person">
      <meta itemprop="image" content="/images/avatar.gif">
      <meta itemprop="name" content="Edison">
      <meta itemprop="description" content="">
    </span>

    <span hidden itemprop="publisher" itemscope itemtype="http://schema.org/Organization">
      <meta itemprop="name" content="共你云雨">
    </span>
      <header class="post-header">
        <h1 class="post-title" itemprop="name headline">
          函数进阶：理解函数的调用
        </h1>

        <div class="post-meta">
            <span class="post-meta-item">
              <span class="post-meta-item-icon">
                <i class="fa fa-calendar-o"></i>
              </span>
              <span class="post-meta-item-text">发表于</span>

              <time title="创建时间：2021-04-09 10:30:08" itemprop="dateCreated datePublished" datetime="2021-04-09T10:30:08+08:00">2021-04-09</time>
            </span>
              <span class="post-meta-item">
                <span class="post-meta-item-icon">
                  <i class="fa fa-calendar-check-o"></i>
                </span>
                <span class="post-meta-item-text">更新于</span>
                <time title="修改时间：2021-04-12 10:29:51" itemprop="dateModified" datetime="2021-04-12T10:29:51+08:00">2021-04-12</time>
              </span>
            <span class="post-meta-item">
              <span class="post-meta-item-icon">
                <i class="fa fa-folder-o"></i>
              </span>
              <span class="post-meta-item-text">分类于</span>
                <span itemprop="about" itemscope itemtype="http://schema.org/Thing">
                  <a href="/categories/%E3%80%8AJavaScript%E5%BF%8D%E8%80%85%E7%A7%98%E7%B1%8D%E3%80%8B%E9%98%85%E8%AF%BB%E7%AC%94%E8%AE%B0/" itemprop="url" rel="index"><span itemprop="name">《JavaScript忍者秘籍》阅读笔记</span></a>
                </span>
            </span>

          

        </div>
      </header>

    
    
    
    <div class="post-body" itemprop="articleBody">

      
        <p>进阶对 JavaScript 函数的理解与运用：this指向、arguments、调用函数的不同方式、函数的上下文</p>
<a id="more"></a>

<h2 id="使用隐式函数参数"><a href="#使用隐式函数参数" class="headerlink" title="使用隐式函数参数"></a>使用隐式函数参数</h2><p>显式的就是自己定义的形参和实参，那么隐式函数参数有哪儿些呢？</p>
<h3 id="arguments-参数"><a href="#arguments-参数" class="headerlink" title="arguments 参数"></a>arguments 参数</h3><pre><code>首先要明确，在之前介绍过的剩余参数其实已经大大削弱了 arguments 的作用，但是了解其工作原理依然重要。</code></pre><ol>
<li>arguments 是传递给函数的所有参数的集合，非真数组</li>
<li>剩余参数接收的是实参，及只包含了没有对应形参的所有实参所组成的真数组，故可以对剩余参数使用 Array 的所有方法。</li>
<li>arguments 具有类似数组的特质，如可以通过下标索引，有 length 属性，但其不过是一个具有类数组结构的对象罢了</li>
<li>arguments 的主要作用在于，能够访问到没有形参对应的实参，这里其实完全可以使用剩余参数替代之。</li>
</ol>
<figure class="highlight js"><table><tr><td class="gutter"><pre><span class="line">1</span><br><span class="line">2</span><br><span class="line">3</span><br><span class="line">4</span><br><span class="line">5</span><br><span class="line">6</span><br><span class="line">7</span><br><span class="line">8</span><br></pre></td><td class="code"><pre><span class="line"><span class="function"><span class="keyword">function</span> <span class="title">myFun</span>(<span class="params">a, b, c</span>) </span>&#123;</span><br><span class="line">	log(<span class="built_in">arguments</span>[<span class="number">0</span>])</span><br><span class="line">	log(<span class="built_in">arguments</span>[<span class="number">1</span>])</span><br><span class="line">	log(<span class="built_in">arguments</span>[<span class="number">2</span>])</span><br><span class="line">	log(<span class="built_in">arguments</span>[<span class="number">3</span>])</span><br><span class="line">	log(<span class="built_in">arguments</span>[<span class="number">4</span>])</span><br><span class="line">&#125;</span><br><span class="line">myFun(<span class="number">1</span>, <span class="number">2</span>, <span class="number">3</span>, <span class="number">4</span>, <span class="number">5</span>) <span class="comment">// 1,2,3,4,5</span></span><br></pre></td></tr></table></figure>

<p>可以看到，即使形参只有三个，但是同过 arguments 也能访问到剩余的实参。下面的剩余参数其实也能实现，所以说剩余参数的出现使得 arguments 的需求大大减少。</p>
<figure class="highlight js"><table><tr><td class="gutter"><pre><span class="line">1</span><br><span class="line">2</span><br><span class="line">3</span><br><span class="line">4</span><br><span class="line">5</span><br></pre></td><td class="code"><pre><span class="line"><span class="function"><span class="keyword">function</span> <span class="title">myFun1</span>(<span class="params">a, b, c, ...d</span>) </span>&#123;</span><br><span class="line">	log(d[<span class="number">0</span>])</span><br><span class="line">	log(d[<span class="number">1</span>])</span><br><span class="line">&#125;</span><br><span class="line">myFun1(<span class="number">1</span>, <span class="number">2</span>, <span class="number">3</span>, <span class="number">4</span>, <span class="number">5</span>) <span class="comment">// 4,5</span></span><br></pre></td></tr></table></figure>

<h3 id="this"><a href="#this" class="headerlink" title="this"></a>this</h3><pre><code>当调用函数时，this 也会被默认传递给函数，代表函数调用相关联的对象。因此，一般称 this 为函数的上下文。真正的理解 this 参数是面向对象 JavaScript 编程的基础。具体在后面讲。</code></pre><h2 id="函数调用"><a href="#函数调用" class="headerlink" title="函数调用"></a>函数调用</h2><pre><code>函数的调用方式对 this 的指向有着至关重要的影响，一般有4个方式：直接调用、new、通过对象调用、apply/call 调用。</code></pre><h3 id="直接调用"><a href="#直接调用" class="headerlink" title="直接调用"></a>直接调用</h3><p>在严格模式下，直接调用函数 this 会指向 undefined 。</p>
<figure class="highlight js"><table><tr><td class="gutter"><pre><span class="line">1</span><br><span class="line">2</span><br><span class="line">3</span><br></pre></td><td class="code"><pre><span class="line">-<span class="function"><span class="keyword">function</span> <span class="title">a</span>(<span class="params"></span>)</span>&#123;</span><br><span class="line">	<span class="keyword">return</span> <span class="keyword">this</span></span><br><span class="line">&#125;() <span class="comment">// this ==&gt; undefined</span></span><br></pre></td></tr></table></figure>

<h3 id="作为对象的方法被调用"><a href="#作为对象的方法被调用" class="headerlink" title="作为对象的方法被调用"></a>作为对象的方法被调用</h3><p>可以看到 this 指向的是 obj 这个对象！</p>
<figure class="highlight js"><table><tr><td class="gutter"><pre><span class="line">1</span><br><span class="line">2</span><br><span class="line">3</span><br><span class="line">4</span><br><span class="line">5</span><br><span class="line">6</span><br><span class="line">7</span><br></pre></td><td class="code"><pre><span class="line"><span class="keyword">let</span> obj = &#123;</span><br><span class="line">	a: <span class="number">1</span>,</span><br><span class="line">	fun1: <span class="function"><span class="keyword">function</span> (<span class="params"></span>) </span>&#123;</span><br><span class="line">		<span class="keyword">return</span> <span class="keyword">this</span></span><br><span class="line">	&#125;</span><br><span class="line">&#125;</span><br><span class="line">log(obj.fun1()) <span class="comment">//&#123; a: 1, fun1: [Function: fun1] &#125;</span></span><br></pre></td></tr></table></figure>

<h3 id="作为构造函数被调用"><a href="#作为构造函数被调用" class="headerlink" title="作为构造函数被调用"></a>作为构造函数被调用</h3><figure class="highlight js"><table><tr><td class="gutter"><pre><span class="line">1</span><br><span class="line">2</span><br><span class="line">3</span><br><span class="line">4</span><br><span class="line">5</span><br><span class="line">6</span><br></pre></td><td class="code"><pre><span class="line"><span class="function"><span class="keyword">function</span> <span class="title">A</span>(<span class="params">name</span>) </span>&#123;</span><br><span class="line">	<span class="keyword">this</span>.name = name</span><br><span class="line">&#125;</span><br><span class="line"><span class="keyword">let</span> a = <span class="keyword">new</span> A(<span class="string">"edison"</span>)</span><br><span class="line">a.__proto__ === A.prototype ? log(<span class="number">1</span>) : log(<span class="number">2</span>) <span class="comment">//1</span></span><br><span class="line">A.prototype.constructor === A ? log(<span class="number">3</span>) : log(<span class="number">4</span>) <span class="comment">//3</span></span><br></pre></td></tr></table></figure>

<p>要探究构造函数中 this 的指向，就要从 new 的过程开始讲起：</p>
<ol>
<li>首先在内存中创建一个新的存储空间</li>
<li>构造函数的 this 指向这个存储空间</li>
<li>接着开始赋值（即构造函数中的属性和方法），其实和面向对象编程时一个道理</li>
<li>返回一个新的实例</li>
</ol>
<p>其实这里涉及到了对象的原型和原型链等问题，后面会深究。在这一小节理解好函数作为构造函数被调用时 this 的指向问题就行。</p>
<h3 id="使用-apply-和-call-方法调用"><a href="#使用-apply-和-call-方法调用" class="headerlink" title="使用 apply 和 call 方法调用"></a>使用 apply 和 call 方法调用</h3><ul>
<li>apply(thisArg,[ArrayArgs])</li>
</ul>
<p>thisArg：必须项，指定一个 this 的值，如下代码 this 指定为 对象 o<br>ArrayArgs：传入的数组参数</p>
<figure class="highlight js"><table><tr><td class="gutter"><pre><span class="line">1</span><br><span class="line">2</span><br><span class="line">3</span><br><span class="line">4</span><br><span class="line">5</span><br><span class="line">6</span><br><span class="line">7</span><br><span class="line">8</span><br><span class="line">9</span><br><span class="line">10</span><br></pre></td><td class="code"><pre><span class="line"><span class="keyword">let</span> o = &#123;</span><br><span class="line">	x: <span class="number">1</span>,</span><br><span class="line">	y: <span class="number">2</span></span><br><span class="line">&#125;</span><br><span class="line"><span class="function"><span class="keyword">function</span> <span class="title">fn</span>(<span class="params">a,b,c</span>) </span>&#123;</span><br><span class="line">	log(a+b+c)</span><br><span class="line">	log(<span class="keyword">this</span>)</span><br><span class="line">&#125;</span><br><span class="line">fn()<span class="comment">//undefined</span></span><br><span class="line">fn.apply(o,[<span class="number">1</span>,<span class="number">2</span>,<span class="number">3</span>])<span class="comment">//6   &#123; x: 1, y: 2 &#125;</span></span><br></pre></td></tr></table></figure>

<ul>
<li>call(thisArg, arg1, arg2, …)</li>
</ul>
<p>thisArg：同样的指定 this 指向<br>arg1：传入函数的参数列表</p>
<figure class="highlight js"><table><tr><td class="gutter"><pre><span class="line">1</span><br><span class="line">2</span><br><span class="line">3</span><br><span class="line">4</span><br><span class="line">5</span><br><span class="line">6</span><br><span class="line">7</span><br><span class="line">8</span><br><span class="line">9</span><br></pre></td><td class="code"><pre><span class="line"><span class="keyword">let</span> o = &#123;</span><br><span class="line">	x: <span class="number">1</span>,</span><br><span class="line">	y: <span class="number">2</span></span><br><span class="line">&#125;</span><br><span class="line"><span class="function"><span class="keyword">function</span> <span class="title">fn</span>(<span class="params">a, b, c</span>) </span>&#123;</span><br><span class="line">	log(a + b + c)</span><br><span class="line">	log(<span class="keyword">this</span>)</span><br><span class="line">&#125;</span><br><span class="line">fn.call(o, <span class="number">1</span>, <span class="number">2</span>, <span class="number">3</span>) <span class="comment">//6 &#123; x: 1, y: 2 &#125;</span></span><br></pre></td></tr></table></figure>

<h2 id="解决函数上下文问题"><a href="#解决函数上下文问题" class="headerlink" title="解决函数上下文问题"></a>解决函数上下文问题</h2><pre><code>由于函数的调用可能会导致 this 的一些问题，在上文提到的 call apply 可以绕过上下文问题，在某些情况下，使用箭头函数和 bind 会更加优雅</code></pre><h3 id="使用箭头函数"><a href="#使用箭头函数" class="headerlink" title="使用箭头函数"></a>使用箭头函数</h3><p>箭头函数除了可以更加优雅的创建函数，还有一个特性：箭头函数没有单独的 this 值，箭头函数的 this 与声明所在的上下文相同。</p>
<figure class="highlight js"><table><tr><td class="gutter"><pre><span class="line">1</span><br><span class="line">2</span><br><span class="line">3</span><br><span class="line">4</span><br><span class="line">5</span><br><span class="line">6</span><br><span class="line">7</span><br><span class="line">8</span><br><span class="line">9</span><br><span class="line">10</span><br><span class="line">11</span><br></pre></td><td class="code"><pre><span class="line"><span class="keyword">let</span> a = &#123;</span><br><span class="line">	myFun: <span class="function"><span class="keyword">function</span> (<span class="params"></span>) </span>&#123;</span><br><span class="line">		log(<span class="keyword">this</span>)</span><br><span class="line">	&#125;,</span><br><span class="line">	myFun1: <span class="function"><span class="params">()</span> =&gt;</span> &#123;</span><br><span class="line"><span class="meta">		"use strict"</span></span><br><span class="line">		log(<span class="keyword">this</span>)</span><br><span class="line">	&#125;</span><br><span class="line">&#125;</span><br><span class="line">a.myFun() <span class="comment">//rhis 指向对象 a</span></span><br><span class="line">a.myFun1() <span class="comment">// this 指向空对象</span></span><br></pre></td></tr></table></figure>

<ul>
<li>箭头函数的 this 指向并不是使用的时候所在的对象，而是其在定义时就已经存在，故也不能使用 call apply bind 等去改变其 this 值。</li>
</ul>
<h3 id="使用-bind"><a href="#使用-bind" class="headerlink" title="使用 bind"></a>使用 bind</h3><pre><code>bind() 方法创建一个新的函数，在 bind() 被调用时，这个新函数的 this 被指定为 bind() 的第一个参数，而其余参数将作为新函数的参数，供调用时使用。</code></pre><h2 id="加餐：手写-apply-call-bind-函数"><a href="#加餐：手写-apply-call-bind-函数" class="headerlink" title="加餐：手写 apply call bind 函数"></a>加餐：手写 apply call bind 函数</h2><p>了解了三个函数的用法和原理，接下来学习一下源码的写法</p>
<ul>
<li>apply(thisArg,[ArrayArgs])</li>
</ul>
<figure class="highlight js"><table><tr><td class="gutter"><pre><span class="line">1</span><br><span class="line">2</span><br><span class="line">3</span><br></pre></td><td class="code"><pre><span class="line"><span class="built_in">Function</span>.prototype.myApply=<span class="function"><span class="keyword">function</span>(<span class="params">context,args</span>)</span>&#123;</span><br><span class="line"></span><br><span class="line">&#125;</span><br></pre></td></tr></table></figure>

<ul>
<li>call(thisArg,arg1,arg2,arg3…)</li>
</ul>
<figure class="highlight js"><table><tr><td class="gutter"><pre><span class="line">1</span><br><span class="line">2</span><br></pre></td><td class="code"><pre><span class="line"></span><br><span class="line"></span><br></pre></td></tr></table></figure>

<ul>
<li>bind</li>
</ul>

    </div>

    
    
    

      <footer class="post-footer">

        


        
    <div class="post-nav">
      <div class="post-nav-item">
    <a href="/2021/04/09/%E5%BE%AE%E4%BF%A1%E5%B0%8F%E7%A8%8B%E5%BA%8F%E6%9C%80%E4%BD%B3%E5%AE%9E%E8%B7%B5%EF%BC%88%E6%9C%80%E6%96%B0%E7%89%88%EF%BC%89/" rel="prev" title="微信小程序最佳实践（最新版）">
      <i class="fa fa-chevron-left"></i> 微信小程序最佳实践（最新版）
    </a></div>
      <div class="post-nav-item">
    <a href="/2021/04/12/%E7%B2%BE%E9%80%9A%E5%87%BD%E6%95%B0%EF%BC%9A%E9%97%AD%E5%8C%85%E5%92%8C%E4%BD%9C%E7%94%A8%E5%9F%9F/" rel="next" title="精通函数：闭包和作用域">
      精通函数：闭包和作用域 <i class="fa fa-chevron-right"></i>
    </a></div>
    </div>
      </footer>
    
  </article>
  
  
  

  </div>


          </div>
          

<script>
  window.addEventListener('tabs:register', () => {
    let { activeClass } = CONFIG.comments;
    if (CONFIG.comments.storage) {
      activeClass = localStorage.getItem('comments_active') || activeClass;
    }
    if (activeClass) {
      let activeTab = document.querySelector(`a[href="#comment-${activeClass}"]`);
      if (activeTab) {
        activeTab.click();
      }
    }
  });
  if (CONFIG.comments.storage) {
    window.addEventListener('tabs:click', event => {
      if (!event.target.matches('.tabs-comment .tab-content .tab-pane')) return;
      let commentClass = event.target.classList[1];
      localStorage.setItem('comments_active', commentClass);
    });
  }
</script>

        </div>
          
  
  <div class="toggle sidebar-toggle">
    <span class="toggle-line toggle-line-first"></span>
    <span class="toggle-line toggle-line-middle"></span>
    <span class="toggle-line toggle-line-last"></span>
  </div>

  <aside class="sidebar">
    <div class="sidebar-inner">

      <ul class="sidebar-nav motion-element">
        <li class="sidebar-nav-toc">
          文章目录
        </li>
        <li class="sidebar-nav-overview">
          站点概览
        </li>
      </ul>

      <!--noindex-->
      <div class="post-toc-wrap sidebar-panel">
          <div class="post-toc motion-element"><ol class="nav"><li class="nav-item nav-level-2"><a class="nav-link" href="#使用隐式函数参数"><span class="nav-number">1.</span> <span class="nav-text">使用隐式函数参数</span></a><ol class="nav-child"><li class="nav-item nav-level-3"><a class="nav-link" href="#arguments-参数"><span class="nav-number">1.1.</span> <span class="nav-text">arguments 参数</span></a></li><li class="nav-item nav-level-3"><a class="nav-link" href="#this"><span class="nav-number">1.2.</span> <span class="nav-text">this</span></a></li></ol></li><li class="nav-item nav-level-2"><a class="nav-link" href="#函数调用"><span class="nav-number">2.</span> <span class="nav-text">函数调用</span></a><ol class="nav-child"><li class="nav-item nav-level-3"><a class="nav-link" href="#直接调用"><span class="nav-number">2.1.</span> <span class="nav-text">直接调用</span></a></li><li class="nav-item nav-level-3"><a class="nav-link" href="#作为对象的方法被调用"><span class="nav-number">2.2.</span> <span class="nav-text">作为对象的方法被调用</span></a></li><li class="nav-item nav-level-3"><a class="nav-link" href="#作为构造函数被调用"><span class="nav-number">2.3.</span> <span class="nav-text">作为构造函数被调用</span></a></li><li class="nav-item nav-level-3"><a class="nav-link" href="#使用-apply-和-call-方法调用"><span class="nav-number">2.4.</span> <span class="nav-text">使用 apply 和 call 方法调用</span></a></li></ol></li><li class="nav-item nav-level-2"><a class="nav-link" href="#解决函数上下文问题"><span class="nav-number">3.</span> <span class="nav-text">解决函数上下文问题</span></a><ol class="nav-child"><li class="nav-item nav-level-3"><a class="nav-link" href="#使用箭头函数"><span class="nav-number">3.1.</span> <span class="nav-text">使用箭头函数</span></a></li><li class="nav-item nav-level-3"><a class="nav-link" href="#使用-bind"><span class="nav-number">3.2.</span> <span class="nav-text">使用 bind</span></a></li></ol></li><li class="nav-item nav-level-2"><a class="nav-link" href="#加餐：手写-apply-call-bind-函数"><span class="nav-number">4.</span> <span class="nav-text">加餐：手写 apply call bind 函数</span></a></li></ol></div>
      </div>
      <!--/noindex-->

      <div class="site-overview-wrap sidebar-panel">
        <div class="site-author motion-element" itemprop="author" itemscope itemtype="http://schema.org/Person">
  <p class="site-author-name" itemprop="name">Edison</p>
  <div class="site-description" itemprop="description"></div>
</div>
<div class="site-state-wrap motion-element">
  <nav class="site-state">
      <div class="site-state-item site-state-posts">
          <a href="/archives/">
        
          <span class="site-state-item-count">124</span>
          <span class="site-state-item-name">日志</span>
        </a>
      </div>
      <div class="site-state-item site-state-categories">
            <a href="/categories/">
          
        <span class="site-state-item-count">21</span>
        <span class="site-state-item-name">分类</span></a>
      </div>
      <div class="site-state-item site-state-tags">
            <a href="/tags/">
          
        <span class="site-state-item-count">19</span>
        <span class="site-state-item-name">标签</span></a>
      </div>
  </nav>
</div>



      </div>

    </div>
  </aside>
  <div id="sidebar-dimmer"></div>


      </div>
    </main>

    <footer class="footer">
      <div class="footer-inner">
        

<div class="copyright">
</div>
  <div class="powered-by">山高水远  一定相见
  </div>
<div></div>
<div class="theme-info">
  <div class="powered-by"></div>
  <span class="post-count">至今已写107.1k字</span>
</div>

        








      </div>
    </footer>
  </div>

  
  <script src="/lib/anime.min.js"></script>
  <script src="/lib/velocity/velocity.min.js"></script>
  <script src="/lib/velocity/velocity.ui.min.js"></script>

<script src="/js/utils.js"></script>

<script src="/js/motion.js"></script>


<script src="/js/schemes/pisces.js"></script>


<script src="/js/next-boot.js"></script>




  















  

  

</body>
</html>
